import Vue from 'vue'
import VueRouter from 'vue-router'

// 开发环境不使用懒加载
const _import = require('./import-' + process.env.NODE_ENV + '.js')

Vue.use(VueRouter)

/**
 * 全局路由无需嵌套sidebar navbar
 */
const globalRouters = [
  {
    path: '/login',
    name: "Login",
    component: _import('Login'),
    meta: {
      isGlobal: true
    }
  },
  {
    path: '/404',
    name: "NotFound",
    component: _import('NotFound'),
    meta: {
      isGlobal: true
    }
  },
  {
    path: '*',
    redirect: { name: 'NotFound' }
  }
]

/**
 * 主路由需嵌套sidebar navbar
 */
const mainRouters = [{
  path: '/',
  name: 'Main',
  redirect: { name: 'CategoryEdit' },
  component: _import('Main'),
  children: [
    {
      path: '/categories',
      name: 'CategoryEdit',
      component: _import('CategoryEdit')
    },
    {
      path: '/categories/edit/:id',
      name: 'CategoryEdit',
      component: _import('CategoryEdit'),
      props: true
    },
    {
      path: '/categories/list',
      name: "CategoryList",
      component: _import('CategoryList')
    },
    {
      path: '/items',
      name: "ItemEdit",
      component: _import('ItemEdit')
    },
    {
      path: '/items/edit/:id',
      name: "ItemEdit",
      component: _import('ItemEdit'),
      props: true
    },
    {
      path: '/items/list',
      name: "ItemList",
      component: _import('ItemList')
    },
    {
      path: '/heroes',
      name: "HeroEdit",
      component: _import('HeroEdit'),
      props: true
    },
    {
      path: '/heroes/edit/:id',
      name: "HeroEdit",
      component: _import("HeroEdit"),
      props: true
    },
    {
      path: '/heroes/list',
      name: "HeroList",
      component: _import('HeroList')
    },
    {
      path: '/articles',
      name: 'ArticleEdit',
      component: _import('ArticleEdit')
    },
    {
      path: '/articles/edit/:id',
      name: "ArticleEdit",
      component: _import('ArticleEdit'),
      props: true
    },
    {
      path: '/articles/list',
      name: "ArticleList",
      component: _import('ArticleList')
    },
    {
      path: '/advertise',
      name: "AdEdit",
      component: _import('AdEdit')
    },
    {
      path: '/advertise/edit/:id',
      name: "AdEdit",
      component: _import('AdEdit'),
      props: true
    },
    {
      path: '/advertise/list',
      name: "AdList",
      component: _import('AdList'),
    },
    {
      path: '/admin_users',
      name: "AdminUserEdit",
      component: _import('AdminUserEdit')
    },
    {
      path: '/admin_users/edit/:id',
      name: "AdminUserEdit",
      component: _import('AdminUserEdit'),
      props: true
    },
    {
      path: '/admin_users/list',
      name: "AdminUserList",
      component: _import('AdminUserList')
    }
  ]
}]


const router = new VueRouter({
  base: process.env.BASE_URL,
  routes: mainRouters.concat(globalRouters)
})


router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token') || ''
  // 全局路由直接访问
  if (to.meta.isGlobal) return next()
  // 未登录跳转登录页面
  if (!token) return next('/login')
  next()
})

export default router